<template>
  <div>
    <div class="video" ref="videos">
      <div class="radar" v-text="num()"></div>
      <div class="time">{{ electron.gettime }}</div>
      <video :width="scratchwidth" :height="scratchheight - 110"></video>
    </div>
    <div class="Recordloading">
      <canvas v-show="!electron.Recordloading" id="audiocanvas"></canvas>
      <div class="div1" v-show="electron.Recordloading">
        <div class="div2">{{ $t('Distinguish') + name }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import path from "path";
import {
  Initclass,
  getProjectTitleFromFilename,
  readerx
} from "../electron.js";
import fs from "fs";
const vm = window.vm;
export default {
  name: "aliplayer",
  data() {
    return {
      name: ".",
      electron: { gettime: "0:0", Recordloading: false }
    };
  },
  props: ["scratchheight", "scratchwidth"],
  watch: {
    scratchheight() {
      this.$refs.videos.style.width = this.scratchwidth + "px";
      this.$refs.videos.style.height = this.scratchheight - 110 + "px";
    }
  },
  mounted() {
    window.electron = new Initclass();
    this.electron = window.electron;
    this.electron.message = this.message;
    const x = process.argv[process.argv.length - 1];
    try {
      if (path.extname(x) === ".sb3") {
        fs.accessSync(x, fs.constants.R_OK);
        fs.readFile(x, (err, file) => {
          vm.loadProject(file);
        });
        process.argv[process.argv.length - 1] = "";
      }
    } catch (_err) {
      this.$message.error(this.$t('文件无法打开'));
    }
    document.addEventListener("drop", e => {
      e.preventDefault();
      e.stopPropagation();

      let fileToUpload = e.dataTransfer.files[0];
      if (fileToUpload && fileToUpload.name) {
        const uploadedProjectTitle = getProjectTitleFromFilename(
          fileToUpload.name
        );
        if (!uploadedProjectTitle) return;
        readerx(fileToUpload);
      }
    });
    document.addEventListener("dragover", function(e) {
      e.preventDefault();
      e.stopPropagation();
    });

    this.$refs.videos.style.width = this.scratchwidth + "px";
    this.$refs.videos.style.height = this.scratchheight - 110 + "px";
    setInterval(this.timeadd, 300);
  },
  methods: {
    message(data) {
      typeof data;
      this.$message.error(this.$t('网络超时'));
    },
    timeadd() {
      switch (this.name) {
        case ".":
          this.name = "..";
          break;
        case "..":
          this.name = "...";
          break;
        case "...":
          this.name = "....";
          break;
        case "....":
          this.name = ".";
          break;
      }
    },
    num() {
      try {
        if (this.electron) {
          return this.electron.num;
        }
        return 1;
      } catch (err) {
        return 1;
      }
    }
  }
};
</script>

<style scoped>
.video {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  background: #000000;
}
.video video {
  position: absolute;
  top: 0;
  left: 0;
}
.Recordloading {
  position: relative;
  overflow: hidden;
  background-color: rgb(39, 74, 97);
  height: 80px;
}
.Recordloading span {
  display: inline-block;
  width: 110px;
}
.div1 {
  position: absolute;
  width: 100%;
  height: 80px;
  top: 0;
  left: 0;
  z-index: 20;
  color: aliceblue;
}
.div2 {
  background-image: url(/img/Recordloading.gif);
  background-position-y: center;
  background-position-x: right;
  width: 200px;
  line-height: 80px;
  background-repeat: no-repeat;
  font-size: 24px;
  margin: 0 auto;
}
#audiocanvas {
  width: 100%;
  height: 80px;
}

.radar {
  z-index: 20;
  display: none;
  background-image: radial-gradient(
    center,
    rgba(32, 255, 77, 0.3) 0%,
    rgba(32, 255, 77, 0) 75%
  );
  background-image: repeating-radial-gradient(
      rgba(32, 255, 77, 0) 5.8%,
      rgba(32, 255, 77, 0) 18%,
      #20ff4d 18.6%,
      rgba(32, 255, 77, 0) 18.9%
    ),
    linear-gradient(
      90deg,
      rgba(32, 255, 77, 0) 49.5%,
      #20ff4d 50%,
      #20ff4d 50%,
      rgba(32, 255, 77, 0) 50.2%
    ),
    linear-gradient(
      0deg,
      rgba(32, 255, 77, 0) 49.5%,
      #20ff4d 50%,
      #20ff4d 50%,
      rgba(32, 255, 77, 0) 50.2%
    );
  width: 203px;
  height: 203px;
  margin: 0 auto;
  border-radius: 50%;
  font-size: 50px;
  line-height: 200px;
  text-align: center;
  border: 1px solid #000000;
  overflow: hidden;
  color: #00ff33;
}

.radar:after {
  content: " ";
  display: block;
  background-image: linear-gradient(
    44deg,
    rgba(0, 255, 51, 0) 50%,
    #00ff33 100%
  );
  width: 50%;
  height: 50%;
  position: absolute;
  top: 0;
  left: 0;
  animation: radar-beam 1s infinite;
  animation-timing-function: linear;
  transform-origin: bottom right;
  border-radius: 100% 0 0 0;
}

@keyframes radar-beam {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}
.time {
  position: absolute;
  bottom: 0;
  z-index: 25;
  width: 50px;
  height: 24px;
  line-height: 24px;
  background-color: rgb(62, 58, 54);
  color: aliceblue;
  font-size: 16px;
  text-align: center;
  left: 0;
}
</style>
